<template name="sandstormAppDetailsPage">
{{#let txt="apps.appDetails.sandstormAppDetailsPage"}}
  {{setDocumentTitle}}
  {{#sandstormTopbarItem name="title" priority=5 topbar=globalTopbar }}{{appTitle}}{{/sandstormTopbarItem}}
  {{#if newGrainIsLoading}}
    {{!-- It's bad style to use the globally defined _grainSpinner, but we get the benefit of blaze
          re-using the live HTML and not causing a flash for the user. --}}
    {{> _grainSpinner ""}}
  {{else}}

  <div class="app-details{{#if isAppInDevMode}} dev-background{{/if}}">
    {{>sandstormAppDetails
       showPublisherDetails=showPublisherDetails
       viewingTrash=viewingTrash
       showToggleTrash=true
       showUninstall=isAppNotInDevMode
       showRestoreGrainBackup=true
       pkg=pkg
       staticHost=staticHost
       keybaseProfile=keybaseProfile
       lastUpdated=lastUpdated
    }}

    {{#if hasNewerVersion}}
    <div class="newer-version">
      <p>{{_ (con txt "newerVersion.explanation")}}</p>
    </div>
    {{/if}}

    {{#if hasOlderVersion}}
    <div class="older-version">
      <p>{{_ (con txt "olderVersion.explanation")}}</p>
      <p><button class="upgradeGrains">{{_ (con txt "olderVersion.upgradeButton")}}</button></p>
    </div>
    {{/if}}

    <div class="search-row">
      <label>
        <span class="search-icon" title="{{_ (con txt "search.hint")}}"></span>
        <input class="search-bar" type="text" placeholder="{{_ (con txt "search.placeholder")}}" value="{{ searchText }}" />
      </label>
    </div>

    {{#if viewingTrash}}
      <p class="trash-explanation">
        {{_ (con txt "trash.explanation")}}
      </p>
    {{/if}}

    {{#let grains=filteredGrains}}
      {{>sandstormGrainTable grains=grains actions=actions
                             onGrainClicked=onGrainClicked _db=_db showHintIfEmpty=1
                             alwaysShowTableHeaders=1 bulkActionButtons=bulkActionButtons}}
      {{#unless grains}}
        {{#if isFiltering}}
          <div class="no-grains">
            <p>{{{_ (con txt "noGrains.notFound")}}}</p>
            {{#if filteredTrashedGrains}}
              <p>{{_ (con txt "noGrains.inTrash")}}
               <button class="toggle-show-trash">{{_ (con txt "noGrains.trashButton")}}</button>
              </p>
            {{/if}}
          </div>
        {{/if}}
      {{/unless}}
    {{/let}}
  </div>
  {{/if}}
{{/let}}
</template>

<template name="sandstormAppDetails">
{{#let txt="apps.appDetails.sandstormAppDetails"}}
  {{!-- Arguments to this template are:
       showPublisherDetails: Boolean.  Show Keybase/PGP infomation.
       showToggleTrash: Boolean. Show the 'view trash' / 'view main list' button.
       showUninstall: Boolean.  Show the uninstall button.
       showRestoreGrainBackup: Boolean.  Show the "Restore grain backup" button.
       pkg: Object shaped like an element from the Packages collection.
       staticHost: String.  The static wildcard host, needed for identicons.
       keybaseProfile: Object shaped like an element from the KeybaseProfiles collection.
       lastUpdated: Date.  The date of publication of the package.
  --}}
  <div class="app-details-widget">
    <div class="app-icon" style="background-image: url('{{appIconSrc}}');"></div>
    <div class="app-details-box">
      <h1 class="app-title">{{appTitle}}</h1>
      <ul class="app-links">
        {{#if website}}<li role="presentation"><a class="website-link" target="_blank" href="{{website}}">{{_ (con txt "websiteLink")}}</a></li>{{/if}}
        <li role="presentation"><a class="app-market-link" target="_blank" href="https://apps.sandstorm.io/app/{{appId}}">{{_ (con txt "appMarketLink")}}</a></li>
        {{#if codeUrl}}<li role="presentation"><a class="source-code-link" target="_blank" href="{{codeUrl}}">{{_ (con txt "sourceCodeLink")}}</a></li>{{/if}}
        {{#if bugReportLink}}<li role="presentation"><a class="bug-report-link" target="_blank" href="{{bugReportLink}}">{{_ (con txt "bugReportLink")}}</a></li>{{/if}}
      </ul>
      <div class="info-row">
        {{#if showToggleTrash}}
          <button class="toggle-show-trash">
            {{#if viewingTrash}}{{_ (con txt "mainListButton")}}{{else}}{{_ (con txt "trashButton")}}{{/if}}
          </button>
        {{/if}}

        {{#if showUninstall}}
        <button class="uninstall-button">{{_ (con txt "uninstallButton")}}</button>
        {{/if}}
        {{#if showRestoreGrainBackup}}
        <button class="restore-button">{{_ (con txt "restoreButton")}}
          <input type="file" style="display:none" accept=".zip">
        </button>
        {{/if}}
        <ul class="package-info">
          <li class="version">
            <span class="label">{{_ (con txt "version.label")}}</span>
            <span class="content">{{marketingVersion}}</span>
          </li>
          <li class="last-update">
            <span class="label">{{_ (con txt "lastUpdate.label")}}</span>
            {{#if lastUpdated}}
            <span class="content">{{dateString lastUpdated}}</span>
            {{else}}
            <span class="content">{{_ (con txt "lastUpdate.unknown")}}</span>
            {{/if}}
          </li>
          {{#if authorPgpFingerprint}}
          <li class="publisher">
            <span class="name">{{_ (con txt "publisher.label")}}</span>
            <button class="show-authorship-button{{#if showPublisherDetails}} expanded{{/if}}">{{publisherDisplayName}} <span class="expanded-icon"></span></button>
          </li>
          {{/if}}
        </ul>
      </div>
      <div class="publisher-details {{#unless showPublisherDetails}}hide{{/unless}}">
        {{#if authorPgpFingerprint}}
        <p class="has-fingerprint">{{_ (con txt "hasFingerprint")}}</p>
        <ul class="publisher-proofs">
          {{#each publisherProofs}}
          <li class="publisher-proof">
            {{#if isPgpKey proofTypeClass}}
              <span class="pgpkey">
                {{#each keyFragments}}<span>{{fragment}}</span>{{/each}}
              </span>
            {{else}}
            <a class="{{proofTypeClass}}"
               target="_blank"
               href="{{linkTarget}}">
                {{linkText}}
            </a>
            {{/if}}
          </li>
          {{/each}}
        </ul>
        {{else}}
        <p class="no-fingerprint">{{_ (con txt "noFingerprint")}}</p>
        {{/if}}
      </div>
    </div>
  </div>
{{/let}}
</template>
